home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / cross / GBDK-2.0.lha / GBDK / lib / mul.s < prev    next >
Text File  |  1998-10-01  |  1KB  |  45 lines

  1.     .include    "global.s"
  2.  
  3.     .area    _CODE
  4.  
  5.     ;; 16-bit multiplication
  6.     ;; 
  7.     ;; Entry conditions
  8.     ;;   BC = multiplicand
  9.     ;;   DE = multiplier
  10.     ;; 
  11.     ;; Exit conditions
  12.     ;;   DE = less significant word of product
  13.     ;;
  14.     ;; Register used: AF,BC,DE,HL
  15. .mul16::
  16. .mulu16::
  17.     LD    HL,#0x00    ; Product = 0
  18.     LD    A,#15        ; Count = bit length - 1
  19.     ;; Shift-and-add algorithm
  20.     ;; If MSB of multiplier is 1, add multiplicand to partial product
  21.     ;; Shift partial product, multiplier left 1 bit
  22. .mlp:
  23.     SLA    E        ; Shift multiplier left 1 bit
  24.     RL    D
  25.     JR    NC,.mlp1    ; Jump if MSB of multiplier = 0
  26.     ADD    HL,BC        ; Add multiplicand to partial product
  27. .mlp1:
  28.     ADD    HL,HL        ; Shift partial product left
  29.     DEC    A
  30.     JR    NZ,.mlp        ; Continue until count = 0
  31.     ;; Add multiplicand one last time if MSB of multiplier is 1
  32.     BIT    7,D        ; Get MSB of multiplier
  33.     JR    Z,.mend        ; Exit if MSB of multiplier is 0
  34.     ADD    HL,BC        ; Add multiplicand to product
  35. .mend:
  36.     LD    D,H        ; DE = result
  37.     LD    E,L
  38.     RET
  39.  
  40. .mul8::
  41. .mulu8::
  42.     LD    B,#0x00        ; Sign extend is not necessary with mul
  43.     LD    D,#0x00
  44.     JP    .mul16
  45.